package cn.edu.ctbu.scoremg.api;

import cn.edu.ctbu.scoremg.constant.REnum;
import cn.edu.ctbu.scoremg.entity.Course;
import cn.edu.ctbu.scoremg.exception.RException;
import cn.edu.ctbu.scoremg.service.CourseService;
import cn.edu.ctbu.scoremg.util.RUtil;
import cn.edu.ctbu.scoremg.vo.QueryObj;
import cn.edu.ctbu.scoremg.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/course")
public class CourseApiController {
    @Autowired
    private CourseService courseService;

    @GetMapping("/list")
    public R<List<Course>> findAll() {
        List<Course> courses = courseService.getAll();
        return RUtil.success(courses);
    }
    @GetMapping("/list0")
    public R<List<Course>> findAll0() {
        List<Course> courses = courseService.getAll();
        return RUtil.success(courses);
        //此时是学生登录
    }

    @GetMapping("/{id}")
    public R<Course> findById(@PathVariable String id) {
        Course course = courseService.findById(id);
        return RUtil.success(course);
    }

    @GetMapping("/findByName")
    public R<List<Course>> findByName(@RequestParam String name) {
        return RUtil.success(courseService.findByName(name));
    }

    @GetMapping("/findByTeacher")
    public R<List<Course>> findByTeacherNo(@RequestParam String tno) {
        return RUtil.success(courseService.findByTeacherNo(tno));
    }

    @PostMapping("/add")
    public R<Course> add(@RequestBody Course course) {
        return RUtil.success(courseService.add(course));
    }

    @PutMapping("/update")
    public R<Course> update(@RequestBody Course course) {
        return RUtil.success(courseService.update(course));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        courseService.delete(id);
        return RUtil.success();
    }

    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<String> ids) {
        try {
            courseService.deleteBatch(ids);
            return RUtil.success();
        } catch (Exception e) {
            return RUtil.error(REnum.DELETE_ERR.getCode(), "批量删除课程失败");
        }
    }

    @PostMapping("/getbypage")
    public R<Page<Course>> getByPage(@RequestBody QueryObj<Course> qObj) {
        System.out.println("接收到的请求数据: " + qObj);

        // 按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");

        Integer pageIndex = 0;
        Integer pageSize = 10;

        if (qObj == null) {
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            Page<Course> courses = courseService.getAll(pageable);
            return RUtil.success(courses.getContent(), courses.getTotalElements());
        } else {
            pageIndex = qObj.getPage() - 1;
            pageSize = qObj.getLimit();

            if (qObj.getData() instanceof Course) {
                Course course = (Course) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withMatcher("tno", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();

                Example<Course> example = Example.of(course, matcher);
                Page<Course> coursePage = courseService.getAll(example, pageable);

                return RUtil.success(coursePage.getContent(), coursePage.getTotalElements());
            } else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}